﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Windows.Media.Imaging;
using System.Collections;
using System.ComponentModel;
using Microsoft.Phone.Tasks;

namespace Cinamon {
  public partial class MovieTimeSelectPage : PhoneApplicationPage {
    BackgroundWorker bw = new BackgroundWorker();
    BackgroundWorker bw1 = new BackgroundWorker();
    Boolean loaded;

    public MovieTimeSelectPage() {
      InitializeComponent();
    }

    private void hyperLinkButton_Buy(object sender, GestureEventArgs e) {
      WebBrowserTask wtb = new WebBrowserTask();
      HyperlinkButton buyTap = sender as HyperlinkButton;
      wtb.Uri = new Uri(buyTap.CommandParameter.ToString(), UriKind.Absolute);
      wtb.Show(); 
      
    }
    // Aegade valimise lehele suunamisel antakse movieId kaasa.
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {
      if (loaded == false) { // kontrollib, kas leht on juba laetud.
        loaded = true;
        if (this.NavigationContext.QueryString.ContainsKey("movieId")) {
          List<Movie> movieList = (App.Current as App).movies.ToList();
          String movieId = NavigationContext.QueryString["movieId"];

          // Alustab Backgroundworkeris informatsiooni laadimist
          bw.DoWork += (s, eventArg) => {
            loadNowInfo(movieId, movieList);
          };
          bw.RunWorkerAsync();
        }
      }

      if (this.NavigationContext.QueryString.ContainsKey("comingSoonMovieId")) {
        String soonId = NavigationContext.QueryString["comingSoonMovieId"];
        List<Movie> comingMovieList = (App.Current as App).comingMovies.ToList();

        // Alustab Backgroundworkeris informatsiooni laadimist
        bw1.DoWork += (s, eventArg) => {
          loadSoonInfo(soonId, comingMovieList);
        };
        bw1.RunWorkerAsync();
      }
    }


    //Aegade vaatamise lehele praeguste filmide info kuvamine.
    public void loadNowInfo(string id, List<Movie> movieList) {

      Dispatcher.BeginInvoke(() => {
        heading.Text = "vali aeg";
      });

      int j = 0;
      foreach (Movie movie in movieList) {
        Movie movieNow = movie; // foreachi sees peab uuesti deklareerima!!!
        if (movieNow.MovieId.Equals(id)) {
          if (j == 0) {
            this.Dispatcher.BeginInvoke(() => {
              timesBox.Items.Add(movieNow);
              BitmapImage myImage2 = new BitmapImage(new Uri(movieNow.MoviePicture, UriKind.RelativeOrAbsolute));
              name.Text = movieNow.MovieName;
              raiting.Text = movieNow.MovieRating;
              pic.Source = myImage2;
              description.Text = movieNow.MovieDescription;
              duration.Text = "Kestus: " + movieNow.MovieLength + "min.";
            });
            j++;
          } else {
            System.Threading.Thread.Sleep(200);
            this.Dispatcher.BeginInvoke(() => {
              timesBox.Items.Add(movieNow);
            });
          }
        }
      }
    }
    //Aegade vaatamise lehele tulevate filmide aegade kuvamine.
    public void loadSoonInfo(string id, List<Movie> comingMovieList) {

      this.Dispatcher.BeginInvoke(() => {
        heading.Text = "info";
      });

      int j = 0;
      foreach (Movie soonMovie in comingMovieList) {
        Movie movieSoon = soonMovie;
        if (movieSoon.MovieId.Equals(id)) {
          if (j == 0) {
            Dispatcher.BeginInvoke(() => {
              BitmapImage myImage = new BitmapImage(new Uri(movieSoon.MoviePicture, UriKind.RelativeOrAbsolute));
              name.Text = movieSoon.MovieName;
              raiting.Text = movieSoon.MovieRating;
              pic.Source = myImage;
              description.Text = movieSoon.MovieDescription;
              duration.Text = "Kestus: " + movieSoon.MovieLength + "min.";
              timesBox.Items.Add(movieSoon);
            });
            j++;
          } else {
            System.Threading.Thread.Sleep(200);
            Dispatcher.BeginInvoke(() => {
              timesBox.Items.Add(movieSoon);
            });
          }
        }
      }
    }

  }



}
